package com.cy.bus.ui;

import com.cy.utils.SysParam;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * 首页Fragment
 * @author CY
 *
 */
public class HomeFragment extends Fragment {

	private WebView webView;
	private String uri = SysParam.read("homeuri");
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		webView = (WebView) view.findViewById(R.id.home_webview);
		initWebView(uri);
		return view;
	}
	
	/**
	 * 初始化WebView
	 */
	@SuppressLint("SetJavaScriptEnabled")
	protected void initWebView(String uri) {
		//支持JavaScript
		webView.getSettings().setJavaScriptEnabled(true);
		webView.loadUrl(SysParam.read("host")+uri);
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Intent intent = new Intent(HomeFragment.this.getActivity(),WebActivity.class);
				intent.putExtra("url", url);
				if(url.equals(SysParam.read("host")+SysParam.read("hotStation")) || url.endsWith(SysParam.read("host")+SysParam.read("hotBusLine"))) {
					intent.putExtra("noRefresh", true);
				}
				HomeFragment.this.startActivity(intent);
				return true;
			}
			
		});
		// 优先使用缓存
		webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
	}
	
}